/**************************************************************************//**
*
* @copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date            Author       Notes
* 2020-12-12      Wayne        First version
*
******************************************************************************/

#include <rtconfig.h>
#include <rtdevice.h>

#if defined(RT_USING_PIN)
#include <drv_gpio.h>

/* defined the LED_R pin: PB13 */
#define LED_R   NU_GET_PININDEX(NU_PB, 13)  //led5

/* defined the LED_Y pin: PB8 */
#define LED_Y   NU_GET_PININDEX(NU_PB, 8)  //led3

#define LED_G   NU_GET_PININDEX(NU_PG, 15)  //LED4



#if !defined(BSP_USING_USBH)
/* defined the Key1 pin: PE10 */
#define KEY_1   NU_GET_PININDEX(NU_PE, 10)

/* defined the Key2 pin: PE12 */
#define KEY_2   NU_GET_PININDEX(NU_PE, 12)

static uint32_t u32Key1 = KEY_1;
static uint32_t u32Key2 = KEY_2;

void nu_button_cb(void *args)
{
    uint32_t u32Key = *((uint32_t *)(args));

    switch (u32Key)
    {
    case KEY_1:
        rt_pin_write(LED_Y, PIN_HIGH);
        break;
    case KEY_2:
        rt_pin_write(LED_Y, PIN_LOW);
        break;
    }
}
#endif

#endif

/* 指向互斥量的指针 */
static rt_mutex_t dynamic_mutex_buff = RT_NULL;

/* 线 程 AD SENSOR  入 口 */
unsigned int ad_value_buff[8];
unsigned int buff_count=0;
void thread_ad_sensor(void* parameter)
{
	  unsigned short int value = 0;
	  
    int result = RT_EOK;
    static rt_adc_device_t adc_device = RT_NULL;
    char *result_str;
	
    int count = 0;
	  char *ptr = RT_NULL;
	
	  adc_device = (rt_adc_device_t)rt_device_find("adc");
    result_str = (adc_device == RT_NULL) ? "failure" : "success";
    rt_kprintf("probe %s %s \n", "adc", result_str);
	  result = rt_adc_enable(adc_device,  0 );
	  result_str = (result == RT_EOK) ? "success" : "failure";
    rt_kprintf("%s channel %d enables %s \n", adc_device->parent.parent.name,  0 , result_str);
	  
                
	
    while (1)
    {   
        rt_mutex_take( dynamic_mutex_buff, RT_WAITING_FOREVER);			
        ad_value_buff[++buff_count%8] = rt_adc_read(adc_device,  0 ); // channel_0
        //rt_kprintf("%s channel %d  read value is %d -- 0x%08X \n", adc_device->parent.parent.name, 0,buff_count%8, ad_value_buff[buff_count%8]);
        rt_mutex_release( dynamic_mutex_buff );
			
			  rt_pin_write(LED_Y, PIN_LOW);
        rt_thread_mdelay(50);    
			  rt_pin_write(LED_Y, PIN_HIGH);
        rt_thread_mdelay(50);

    }
}

extern int mqtt_start_user( );
extern int mqtt_publish_user( char *pbuff, size_t len );
char air_buff[128];
/*     */
void thread_mqtt(void* parameter)
{
    int count = 0;
	  float  air_q = 0;
	  int i;
	  unsigned  int average_value ;
	  int ret;
	  int len;
	  char *ptr = RT_NULL;
	  
	  rt_thread_mdelay(1000);
	  mqtt_start_user( );
    while (1)
    {
        rt_mutex_take( dynamic_mutex_buff, RT_WAITING_FOREVER);		
			  average_value = 0;
			  for( i = 0 ; i < 8 ; i++ )
	      {	 	  
		        average_value +=  ad_value_buff[i];
	      } 
				average_value = average_value/8;
							
        rt_mutex_release( dynamic_mutex_buff );
				
				
				len = rt_snprintf ( air_buff ,128, "#air quality:%d\r\n" ,average_value);
			  //rt_thread_mdelay(2);
			  mqtt_publish_user( air_buff,  len  );
				
        rt_thread_mdelay(5000);
    }
}



int main(int argc, char **argv)
{
	  rt_thread_t thread_ad_sensor_ptr;
	  rt_thread_t thread_mqtt_ptr;
    rt_err_t result; 
	  
	  /* 创建一个动态互斥量 */
    dynamic_mutex_buff = rt_mutex_create("dmutex", RT_IPC_FLAG_PRIO);
    if ( dynamic_mutex_buff == RT_NULL)
    {
        rt_kprintf("create dynamic mutex failed.\n");
        return -1;
    }
		
	  thread_ad_sensor_ptr = rt_thread_create("sensor",thread_ad_sensor, RT_NULL,1024, 9, 30);
    /* 启 动 线 程 */
    if (thread_ad_sensor_ptr != RT_NULL) rt_thread_startup(thread_ad_sensor_ptr);
	
	  thread_mqtt_ptr = rt_thread_create("sensor",thread_mqtt, RT_NULL,1024, 8, 30);
    /* 启 动 线 程 */
    if (thread_mqtt_ptr != RT_NULL) rt_thread_startup(thread_mqtt_ptr);
	
	
	  rt_thread_mdelay( 50 );
	
	
#if defined(RT_USING_PIN)
    int counter = 1000;

    /* set LED_R pin mode to output */
    rt_pin_mode(LED_R, PIN_MODE_OUTPUT);

    /* set LED_Y pin mode to output */
    rt_pin_mode(LED_Y, PIN_MODE_OUTPUT);

	   /* set LED_Y pin mode to output */
    rt_pin_mode(LED_G, PIN_MODE_OUTPUT);
	  rt_pin_write(LED_G, PIN_HIGH);
	
#if !defined(BSP_USING_USBH)
    /* set KEY_1 pin mode to input */
    rt_pin_mode(KEY_1, PIN_MODE_INPUT_PULLUP);

    /* set KEY_2 pin mode to output */
    rt_pin_mode(KEY_2, PIN_MODE_INPUT_PULLUP);

    rt_pin_attach_irq(KEY_1, PIN_IRQ_MODE_FALLING, nu_button_cb, &u32Key1);
    rt_pin_irq_enable(KEY_1, PIN_IRQ_ENABLE);

    rt_pin_attach_irq(KEY_2, PIN_IRQ_MODE_FALLING, nu_button_cb, &u32Key2);
    rt_pin_irq_enable(KEY_2, PIN_IRQ_ENABLE);
#endif

    while ( 1 )
    {
        rt_pin_write(LED_R, PIN_HIGH);
        rt_thread_mdelay(100);
        rt_pin_write(LED_R, PIN_LOW);
        rt_thread_mdelay(100);
    }

#endif

    return 0;
}
